package snakesladers.view;

import minidraw.boardgame.Command;
import snakesladders.domain.Game;
import snakesladders.domain.Square;

public class MoveCommand implements Command {

	private int fx,fy,tx,ty;
	private Game game;

	public MoveCommand (Game game) {
		this.game = game;
	}
	
	@Override
	public boolean execute() {
		System.out.println("Moving from ("+fx+","+fy+") to ("+tx+","+ty+")");
		
		int from = indexFromXY(fx, fy);
		int to = indexFromXY(tx, ty);
		Square f = new Square(from), t = new Square(to);
		boolean value = game.move(0, f, t);
		
		//false if move is invalid - TKJ
		return true;
	}

	@Override
	public void setFromCoordinates(int fromX, int fromY) {
		fx = fromX;
		fy = fromY;
	}

	@Override
	public void setToCoordinates(int toX, int toY) {
		tx = toX;
		ty = toY;
	}

	private int indexFromXY(int x, int y) {
		System.out.println("  (row,colum) = ("+x+","+y+")");
		// sqc = colum coordinate of the square; sqr = row coordiante if the square
		int sqc = (x - 17)/92, sqr = (472 - y) / 92;
		System.out.println("  (row,colum) = ("+sqr+","+sqc+")");
		int squareindex = 1 + sqr*7 + sqc;
		if (squareindex > 17) { squareindex--; }
		System.out.println("  ---> index; =" + squareindex);
		return squareindex;
	}
}
